package com.kfm.singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class SimpleSingleton {

    public static void main(String[] args) {

        InnerClassSingleton instance = InnerClassSingleton.getInstance();

        System.out.println(instance);

//        Class<InnerClassSingleton> innerClassSingletonClass = InnerClassSingleton.class;
//        try {
//            Constructor<InnerClassSingleton> constructor = innerClassSingletonClass.getDeclaredConstructor();
//            constructor.setAccessible(true);
//            InnerClassSingleton innerClassSingleton = constructor.newInstance();
//            InnerClassSingleton innerClassSingleton1 = constructor.newInstance();
//            System.out.println(instance);
//            System.out.println(innerClassSingleton);
//            System.out.println(innerClassSingleton1);
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
    }
}
//懒汉模式
class LazySingleton{
    private LazySingleton(){}

    private static LazySingleton instance;

    public static LazySingleton getInstance(){
        if (instance == null){
            instance = new LazySingleton();
        }
        return instance;
    }
}
//饿汉模式
class HungrySingleton{
    private HungrySingleton(){}

    private static final HungrySingleton instance = new HungrySingleton();

    public static HungrySingleton getInstance(){
        return instance;
    }
}
//枚举
enum EnumSingleton{
    A,S,D,F;
}

//静态内部类
class InnerClassSingleton{
    private InnerClassSingleton(){
        if (StaticInnerClass.instance != null){
            throw new RuntimeException("错误使用");
        }
    }

    private static class StaticInnerClass{
        private static final InnerClassSingleton instance = new InnerClassSingleton();
    }
    public static InnerClassSingleton getInstance(){
        return StaticInnerClass.instance;
    }
}